# -*- coding: utf-8 -*-
# __init__.py create by kumiler
# on 2022/6/24 17:25
# desc
from datetime import timedelta
from utils.operators.rich_sql_sensor import RichSqlSensor
#from jms.dm.dm_waybill_customer_complaint_version2.complaint_0600.dm_waybill_customer_complaint_agg_new_dt import jms_dm__dm_waybill_customer_complaint_agg_new_dt
#from jms.dm.dm_waybill_customer_complaint_version2.complaint_0600.dm_waybill_customer_complaint_detail_new_dt import jms_dm__dm_waybill_customer_complaint_detail_new_dt
from time import time
#doris表名
doris_table = "dwd_waybill_customer_complaint_detail_new_dt"
#label名
label = f"{doris_table}_{int(time())}"
#hive中表名
hive_table = "dm_waybill_customer_complaint_detail_new_dt"
#hdfs文件路径
hdfs_path ="hdfs://10.33.48.5:8020/dw/hive/jms_dm.db/external/"
timeout = timedelta(minutes=60).seconds
#判断当天分区是否存在
file_path = f'"{hdfs_path}{hive_table}/dt={{{{ execution_date | cst_ds }}}}/*"'
partition_lists = []
path_lists = []
for i in range(0, 31):
    if i == 0:
        partition_lists.append("p{{ execution_date | cst_ds_nodash }}")
        path_lists.append(file_path)
    else:
        partition_lists.append("p{{ execution_date | date_add(-" + str(i) + ") | cst_ds_nodash }}")
        path_lists.append(f'"{hdfs_path}{hive_table}/dt={{{{ execution_date | date_add(-{i}) | cst_ds }}}}/*"')

partition_str = ",".join(partition_lists)
path_str = ",".join(path_lists[::-1])
doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris_uat',
    pre_sql=f"""
                 TRUNCATE TABLE jms_dwd.{doris_table} PARTITION ({partition_str});
                 LOAD LABEL jms_dwd.{label} (
                     DATA INFILE({path_str})
                     INTO TABLE {doris_table}
                     FORMAT AS 'PARQUET'
                     (data_time,work_type,accept_time,waybill_no,first_type_code,first_type_name,second_type_code,second_type_name,create_time,trans_offices,deliver_type,accept_network_code,accept_network_name,accept_network_type,accept_proxy_code,accept_proxy_name,transfer_network_code,transfer_network_name,transfer_network_types,transfer_network_type,duty_network_code,duty_network_name,duty_network_type,duty_proxy_code,duty_proxy_name,work_order_reason_step,is_delete,work_order_no,delivery_code,delivery_name,tail_code,tail_name,problem_desc,work_order_channel,express_cabinet,incoming_time,trans_time,his_delete,work_order_source_id,remarks,create_day,order_source_code,order_source_name,duty_type,provider_id,provider_desc,city_id,city_desc,district_code,district_desc,district_id,transfer_proxy_code,transfer_proxy_name,transfer_provider_id,transfer_provider_desc,transfer_city_id,transfer_city_desc,transfer_district_code,transfer_district_desc,transfer_district_id,record_group,send_back_code,reback_transfer_falg,area_code,area_name,area_id,send_type_address,network_code,network_name,network_type
                    )
                 )
                 WITH BROKER '{{{{ var.json.doris_brokers_uat | random_choice }}}}'
                 PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120),
)
#doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt << jms_dm__dm_waybill_customer_complaint_detail_new_dt



